//
//  ViewController.m
//  UICollectionViewFlowLayoutTest
//
//  Created by Rocky on 16/4/12.
//  Copyright © 2016年 Rocky. All rights reserved.
//

#import "ViewController.h"
#import "FlowLayout.h"
#import "CircelLayout.h"
//
@interface ViewController ()<UICollectionViewDelegateFlowLayout>
{
    NSInteger collectionView2Num;
}
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView2;

@end
static NSString * const reuseIdentifier = @"Cell";

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    collectionView2Num = 20;
    FlowLayout *flowLayout=[[FlowLayout alloc] init];
    [flowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
    [self.collectionView setCollectionViewLayout:flowLayout];
    //[self.collectionView setBackgroundColor:[UIColor clearColor]];
    [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];
    //
    [self.collectionView2 setBackgroundColor:[UIColor clearColor]];
    CircelLayout* layout = [[CircelLayout alloc]init];
    self.collectionView2.collectionViewLayout = layout;
    [self.collectionView2 registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];
    //[self.view addSubview:self.collectionView];
}

/*
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(50, 50);;
}
 */
/*
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    
    return UIEdgeInsetsMake(20, 20, 20, 20);
}
*/
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
    return UIInterfaceOrientationIsLandscape(toInterfaceOrientation);
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {

    return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    
    if ([collectionView isEqual:self.collectionView2])
    {
        return collectionView2Num;
    }
    NSLog(@"====numberOfItemsInSection===");
    return 20;
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"=viewForSupplementaryElementOfKind==");
    
    UICollectionViewCell *view = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    view.backgroundColor = [UIColor greenColor];
    return view;
}



- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    cell.backgroundColor=[UIColor colorWithRed:arc4random_uniform(255)/255.0 green:arc4random_uniform(255)/255.0 blue:arc4random_uniform(255)/255.0 alpha:1.0f];;
    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if ([collectionView isEqual:self.collectionView2])
    {
        NSLog(@"====didSelectItemAtIndexPath=====");
        collectionView2Num -- ;
        [collectionView performBatchUpdates:^
         {
             [collectionView deleteItemsAtIndexPaths:[NSArray arrayWithObject:indexPath]];
         }completion:nil];
        
    }
}
- (IBAction)btnAdd_click:(id)sender
{
    collectionView2Num ++;
    __weak typeof(self)weakSelf = self;
    //用UIView Animation 包住performBatchUpdates可以使view的Animation代替collectionView默认的动画
    [UIView animateWithDuration:0.6 delay:0 usingSpringWithDamping:0.5 initialSpringVelocity:0.3 options:UIViewAnimationOptionCurveEaseInOut animations:^{
          [weakSelf.collectionView2 performBatchUpdates:^{
              NSIndexPath *indexPath =[NSIndexPath indexPathForRow:0 inSection:0];
              [weakSelf.collectionView2 insertItemsAtIndexPaths:[NSArray arrayWithObject:indexPath]];
          }completion:nil];
         
     }completion:nil];
}


/*
-(CGSize)collectionViewContentSize
{
    CGFloat height=  ceil([[self collectionView]  numberOfItemsInSection:0]/5)*SCREEN_WIDTH/2;
    return  CGSizeMake(SCREEN_WIDTH, height);
    
}//返回contentsize的总大小
*/
-(BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds
{
    
    return YES;
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
